<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/9/20
 * Time: 9:53
 */

namespace Wechat\Controller;


class WebappController extends CommonController
{
    //主页
    public function index()
    {
        //微信分享
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);
        //获取首页轮播图
        $lun = M('slide')->where(['slide_cid'=>8])->select();
        //获取轻松一刻数据
        $julebu = M('happy')->order(['sort'=>'desc'])->limit(3)->select();
        $this->assign(['julebu'=>$julebu,'lun'=>$lun]);
        $this->display();
    }
    //主页求租
    public function qiuzu()
    {
        $qiuzu = M('zhuanzulin')->where(['build'=>2])->order('id desc')->limit(3)->select();
        /*
        $qiuzu = [
            'qiuzu'=>$qiuzu
        ];
        */
        echo json_encode($qiuzu);
    }
    //主页求买
    public function qiumai()
    {
        $qiumai = M('zhuanershou')->where(['build'=>2])->order('id desc')->limit(3)->select();
        /*
        $qiumai = [
            'qiumai'=>$qiumai
        ];
        */
        echo json_encode($qiumai);
    }
    //主页二手
    public function ershou()
    {
        $ershou = M('zhuanershou')->order('id desc')->limit(3)->select();
        /*
        $ershou = [
            'ershou'=>$ershou
        ];
        */
        echo json_encode($ershou);
    }
    //主页-出租
    public function chuzu()
    {
        $chuzu = M('zhuanzulin')->where(['build'=>1])->order('id desc')->limit(3)->select();
        /*
        $chuzu = [
            'chuzu'=>$chuzu
        ];
        */
        echo json_encode($chuzu);
    }
    //主页新房
    public function xinfang()
    {
        $xinfang = M('zhuanxinfang')->order('id desc')->limit(3)->select();
        /*
        $xinfang = [
            'xinfang'=>$xinfang
        ];
        */
        echo json_encode($xinfang);
    }
    //我要代言
    public function erwei()
    {
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];

        //调用微信生成的二维码
        $ti = $this->wechatcode();
        $ticketuc = urlencode($ti['ticket']);
        //获取到分享出去人的openid
        $openid = i('get.openid');
        //拼接二维码
        $erwei = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$ticketuc;

        //获取用户的信息
        $user = M('wechatuser')->where(['openid'=>$openid])->find();

        //自定义分享
        (new WechatController())->zidingfenxiang($url);
        $this->assign(['erwei'=>$erwei,'v'=>$user]);
        $this->display();
    }
    //智能锁页面
    public function lock()
    {
        //如果是点进去别人分享的 那就 给自己增加积分 通过openid
        $openid = i('get.openid');

        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI].'&openid='.$_SESSION['wechatopenid'];
        //自定义分享
        (new WechatController())->zidingfenxiang($url);

        //调用微信生成的二维码
        $ti = $this->wechatcode();
        $ticketuc = urlencode($ti['ticket']);
        //拼接二维码
        $erwei = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$ticketuc;

        $daiyan = M('webbuy')->where(['openid'=>$_SESSION['wechatopenid']])->find();
        $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
        $this->assign(['daiyan'=>$daiyan,'user'=>$user,'erwei'=>$erwei]);
        $this->display();
    }
    //智能锁代言
    public function daiyan()
    {
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['yinhang']))) {
                $this->error('请选择所属银行');
            }
            if(trim(empty($post['shoukuan']))) {
                $this->error('请输入收款账号');
            }
            if(trim(empty($post['huming']))) {
                $this->error('请输入账号户名');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入手机号');
            }
            if(!isset($_SESSION['wechatopenid'])){
                $this->error('请在微信客户端打开链接');
            }
            $data = [
                'yinhang'=>$post['yinhang'],
                'shoukuan'=>$post['shoukuan'],
                'huming'=>$post['huming'],
                'phone'=>$post['phone'],
                'openid'=>$_SESSION['wechatopenid'],
                'time'=>time()
            ];
                if(M('webdaiyan')->add($data)){
                    $this->success('申请成功',u('Webapp/lock'));
                }else{
                    $this->error('失败！');
                }
        }else{
            $this->display();
        }
    }
    //智能锁购买
    public function buy()
    {
        if(IS_POST){
            $post = i('post.');
            if(!isset($_SESSION['wechatopenid'])){
                $this->error('请在微信客户端执行操作');
            }
            $data = [
                'color'=>$post['color'],
                'time'=>time(),
                'openid'=>$_SESSION['wechatopenid'],
                'dizhi'=>$post['dizhi'],
                'phone'=>$post['phone']
            ];
            if($data['phone']!=NULL){
                if(M('webbuy')->add($data)){
                    echo json_encode('success');
                }else{
                    echo json_encode('fail');
                }
            }
        }else{
            $this->display();
        }
    }

    //参考函数
    public function once()
    {
        $data = M('zhuanershou')->select();
        foreach ($data as $k=>$v){
            if($data[$k]['data']==''){
                M('zhuanershou')->where(['id'=>$v['id']])->save(['data'=>'ershou']);
            }
        }
    }
}